package com.msti.controller;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.mapping.Array;

import com.msti.entity.Batch;
import com.msti.entity.Course;
import com.msti.entity.Student;
import com.msti.service.BatchMgtDAO;
import com.msti.service.BatchMgtDAOImpl;
import com.msti.service.CourseMgtDAO;
import com.msti.service.CourseMgtDAOImpl;
import com.msti.utility.Utility;

/**
 * Servlet implementation class StudentDetail
 */
public class StudentDetail extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private final static String FILE_NAME = "C:/Res/Files/hpf.dat";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentDetail() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Student student = new Student();
		student.setAddress(request.getParameter("address"));
		student.setCDCNO(request.getParameter("CDCNO"));
		student.setDateOfBirth(Utility.stringToDate(request.getParameter("dateOfBirth")));
		student.setFullName(request.getParameter("fullName"));// should be change
		int hid = Utility.getId(FILE_NAME); 
		student.setIdHelper(hid);// should be change
		Utility.writeId(hid, FILE_NAME);
		student.setNICNO(request.getParameter("NICNO"));
		student.setPassportNo(request.getParameter("passportNo"));
		student.setPhone(request.getParameter("phone"));
		student.setStudentID(Utility.getStudentId(hid));
		BatchMgtDAO batchMgtDAO = new BatchMgtDAOImpl();
		List<Batch> batchs = new ArrayList<Batch>();
		batchs = batchMgtDAO.getAllBatch();
		CourseMgtDAO courseMgtDAO = new CourseMgtDAOImpl();
		List<Course> courses = new ArrayList<Course>();
		courses = courseMgtDAO.getAllCourse();
		List<Course> mappingCourses = getMappingCourses(batchs, courses);
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("courseSelection.jsp");
		request.setAttribute("student", student);
		request.setAttribute("courses", mappingCourses);
		request.setAttribute("batch", batchs);
		request.getSession().setAttribute("stdDob", request.getAttribute("dateOfBirth"));
		//load batches and redirect courses which are mapping to the batch id
		requestDispatcher.forward(request, response);
	}

	private List<Course> getMappingCourses(List<Batch> batchs, List<Course> courses) {
		List<Course> mappingCourses = new ArrayList<Course>();
		Date currentDate = new Date();
		for(Course c : courses){
			for(Batch b : batchs){
				if(b.getCourse_id() == c.getId() && Utility.getMappingDateAsString(currentDate) < Utility.getMappingDateAsString(b.getStartDate()) ){
					mappingCourses.add(c);
					System.out.println(b.getId());
					System.out.println(currentDate);
					System.out.println(b.getCompleatedDate());
				}
			}
		}
		//System.out.println(mappingCourses);
		return mappingCourses;
	}

}
